//
//  ShouXinRecordVC.swift
//  物料商行
//
//  Created by HaoDong Hong on 2017/9/27.
//  Copyright © 2017年 HaoDong Hong. All rights reserved.
//

import UIKit
import SVProgressHUD

class ShouXinRecordVC: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    var items = [ShouXinRecord]()
    /// 1代表商户，2代表买家
    var type: Int!
    let helper = MaterialHelper()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0)
        tableView.register(UINib(nibName: "ShouXinRecordCell", bundle: nil), forCellReuseIdentifier: "ShouXinRecordCell")
        requestRecords(with: type)
    }
    func requestRecords(with type: Int) {
        SVProgressHUD.show()
        helper.getShouXinRecord(with: type) { (error, records) in
            if error != nil {
                SVProgressHUD.showError(withStatus: error.debugDescription)
            } else {
                self.items = records!
                self.tableView.reloadData()
            }
            SVProgressHUD.dismiss(withDelay: 1.0)
        }
    }
}

extension ShouXinRecordVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ShouXinRecordCell") as! ShouXinRecordCell
        let row = indexPath.row
        if type == 1 {
            let item = items[row] as! SalerShouXinRecord
            cell.dateLabel.text = item.createAt
            cell.userLabel.text = item.userId
            cell.priceLabel.text = item.limit
            cell.qixianLabel.text = item.date
            cell.beizhuLabel.text = ""
        } else {
            let item = items[row] as! BuyerShouXinRecord
            cell.dateLabel.text = item.createAt
            cell.userLabel.text = item.buyerPhone
            cell.priceLabel.text = item.limit
            cell.qixianLabel.text = item.date
            cell.beizhuLabel.text = ""
        }
        return cell
    }
}
